<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include('base.php');

  class ScheduleController extends BaseController
  {
    var $_name = 'schedule';
    
    function add(){
    
      $document =& JFactory::getDocument();
      
      $view = &$this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel('conference','conferencemodel'));
      $view->setModel($this->getModel('venue','venuemodel'));
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel($this->_name), true);
      
      $view->displayAdd();
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' ); 
    
      if($cids === null)
      {
                JError::raiseError(500, 'cid parameter missing from the request');
      }    
    
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('conference','conferencemodel'));
      $view->setModel($this->getModel('venue','venuemodel'));
      $view->setModel($this->getModel('team','teammodel'));
      
      $view->displayEdit();  
    }
    
    function save()
    {
      $data = JRequest::get('POST');
      $data['notes'] = JRequest::getVar( 'notes', '', 'post', 'string', JREQUEST_ALLOWHTML );
       
      $model = & $this->getModel($this->_name); 
      
      $model->save($data);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
    
    function ajx_select_conferences()
    {
      $team            = $this->getModel('team', 'teammodel');
      $conference_team = $this->getModel('conferenceteam', 'conferenceteammodel');
      
      $conference_id   = JRequest::getVar('conference_id');
      $conference_teams= $conference_team->get_by_conference_id($conference_id);
      
      $teams = array();
      foreach($conference_teams as $ct)
        array_push($teams, $team->get($ct->team_id));
      
      $result  = JHTML::_('select.genericlist', $teams, 'first_team_id', 'size=10', 'id', 'name', null);
      $result .= "&nbsp;&nbsp;VS.&nbsp;&nbsp;";
      $result .= JHTML::_('select.genericlist', $teams, 'second_team_id', 'size=10', 'id', 'name', null);
      echo $result;
    }

  }
?>